package com.shixj.easy.spider.factory;

import com.shixj.easy.spider.model.Novel;
import com.shixj.easy.spider.model.RegexConfig;
import com.shixj.ef.constant.StringCons;
import com.shixj.ef.utils.FetchHelper;
import com.shixj.ef.vo.FetchResult;
import org.jsoup.nodes.Document;

/**
 * Created by shixj on 2018/1/15.
 */
public class DefaultNovelFactory implements EntityFactory {

    /**
     * 转换novel封面信息
     *
     * @param result
     * @return
     */
    @Override
    public Novel build(FetchResult result,RegexConfig regexConfig) {
        Document document = result.getDocument();
        Novel novel = new Novel();
        //sourceId
        String sourceId = FetchHelper.subByRegex(result.getUrl(), regexConfig.getNovelSourceIdRegex(),0);
        novel.setSourceId(sourceId);
        //novelName
        novel.setNovelName(getPartText(document, regexConfig.getNovelNameRegex()));
        String authorText = getPartText(document, regexConfig.getAuthorRegex());
        novel.setAuthor(authorText.matches("作.*者：.*") ? authorText.replaceAll(".*：", StringCons.EMPTY) : authorText);
        novel.setCover(document.select(regexConfig.getCoverRegex()).first().attr("src"));
        novel.setDespriction(getPartHtml(document,regexConfig.getChapterDescriptionRegex()));
        return novel;
    }
}
